
import { Reducer, Effect } from 'umi';

import { queryRule } from '@/pages/TableList/service';

export interface QueryStateType {
  status?: 'ok' | 'error';
  type?: string;
  list?:[],
  total?:0,
  currentAuthority?: 'user' | 'guest' | 'admin';
}

export interface QueryListModelType {
  namespace: string;
  state: QueryStateType;
  effects: {
    query: Effect;
  };
  reducers: {
    setListData: Reducer<QueryStateType>;
  };
}

const Model: QueryListModelType = {
  namespace: 'queryList',

  state: {
    list:[],
    total:0,
    status: undefined,
  },

  effects: {
    *query({ payload }, { call, put }) {
      const response = yield call(queryRule, payload);
      if(response.code==='2000'){
        yield put({
          type: 'setListData',
          payload: response.data,
        });
      }

      // Login successfully
    },
  },

  reducers: {
    setListData(state, { payload }) {
      console.log('state',state);
      console.log('payload',payload);
      const {list ,total} = payload
      return {
        ...state,
        list,
        total,
      };
    },
  },
};

export default Model;
